<template>
  <div class="search-container">
    <input
      type="text"
      v-model="searchQuery"
      placeholder="请输入搜索关键字"
      @keyup.enter="onSearch"
      ref="searchInput"
      :class="{ 'has-value': searchQuery }"
    />
    <button @click="onSearch">搜索</button>
    <div class="search-icon" @click="focusSearchInput">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
        <path d="M497.943 465.397L362.76 330.214c35.354-43.59 56.042-98.473 56.042-157.923C418.802 74.193 325.61 0 213.001 0S7.2 74.193 7.2 172.291s93.193 172.292 205.792 172.292c45.983 0 88.615-15.838 122.616-42.337l135.184 135.185a24.055 24.055 0 0034.02 0 24.049 24.049 0 000-34.019zM213.001 327.471c-83.458 0-150.791-66.026-150.791-147.18 0-81.155 67.333-147.181 150.791-147.181 83.457 0 150.79 66.026 150.79 147.181 0 81.154-67.333 147.18-150.79 147.18z"/>
      </svg>
    </div>
  </div>
</template>

<script>
export default {
  name: "Search",
  data() {
    return {
      searchQuery: "",
    };
  },
  methods: {
    onSearch() {
      this.$emit("search", this.searchQuery);
    },
    focusSearchInput() {
      this.$refs.searchInput.focus();
    },
  },
};
</script>

<style scoped>


input {
  margin-right: 10px;
  border-radius: 5px;
  border: none;
  padding: 10px;
  width: 550px;
  background-color: #f8f8f8;
  font-size: 16px;
  color: #333;
  transition: all 0.3s ease;
}

input:focus {
  outline: none;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

button {
  background-color: rgba(21,219,145,.8);
  color: white;
  border-radius: 5px;
  border: none;
  padding: 10px 20px;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
}

button:hover {
  background-color: rgba(21,219,145,.8);
}

.has-value + .search-icon {
  opacity: 1;
  visibility: visible;
  transform: scale(1);
  transition-delay: 0.3s;
}

.search-icon {
  position: absolute;
  right: 45px;
  opacity: 0;
  visibility: hidden;
  transform: scale(0.5);
  transition: all 0.3s ease;
  cursor: pointer;
}
.search-container {
  display: flex;
  align-items: center;
  position: relative;
  border: 2px solid rgba(21,219,145,.8);
  border-radius: 10px;
  padding: 5px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
 /*opacity: 0.7; !* 调整透明度 *!*/
  /*background-color: rgba(255, 255, 255, 0.9);*/
  /* 填充边框 */
  background-color: rgba(21,219,145,.8);
  background-clip: padding-box;
}
.search-icon svg {
  width: 20px;
  height: 20px;
}
</style>